package com.hy.activiti.controller;


import com.github.pagehelper.Page;
import com.hy.activiti.service.IActTaskService;
import com.hy.framework.web.controller.BaseController;
import com.hy.framework.web.domain.AjaxResult;
import com.hy.framework.web.page.PageDomain;
import com.hy.framework.web.page.TableDataInfo;
import com.hy.framework.web.page.TableSupport;
import com.hy.project.activiti.domain.dto.ActTaskDTO;
import com.hy.project.activiti.domain.dto.ActWorkflowFormDataDTO;
import org.activiti.api.task.runtime.TaskRuntime;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;


@RestController
@RequestMapping("/task")
public class TaskController extends BaseController {
    @Autowired
    private TaskRuntime taskRuntime;

    @Autowired
    private IActTaskService actTaskService;



    //获取我的代办任务
    @GetMapping(value = "/list")
    public TableDataInfo getTasks() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<ActTaskDTO> hashMaps = actTaskService.selectProcessDefinitionList(pageDomain);
         return getDataTable(hashMaps);


    }

    /**
     * 获取已办任务
     * @param page
     * @param procDefKey 流程定义标识
     * @return
     */
    @GetMapping(value = "historic")
    public TableDataInfo historicList(@RequestBody ActTaskDTO actTaskDTO) throws Exception {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<ActTaskDTO> hashMaps = actTaskService.historicList(pageDomain , actTaskDTO);
        return getDataTable(hashMaps);
    }

    /**
     * 获取流转历史列表
     * @param procInsId 流程实例
     * @param startAct 开始活动节点名称
     * @param endAct 结束活动节点名称
     */
    @GetMapping(value = "histoicFlow")
    public String histoicFlow(@RequestBody ActTaskDTO actTaskDTO,
                              String startAct, String endAct, Model model){
        if (actTaskDTO.getProcIns() != null && StringUtils.isNotBlank(actTaskDTO.getProcIns().getId() )){
            List<ActTaskDTO> histoicFlowList = actTaskService.histoicFlowList(actTaskDTO.getProcIns().getId(), startAct, endAct);
            model.addAttribute("histoicFlowList", histoicFlowList);
        }
        return "modules/act/actTaskHistoricFlow";
    }

    //渲染表单
    @GetMapping(value = "/formDataShow/{taskID}")
    public AjaxResult formDataShow(@PathVariable("taskID") String taskID) {

        return AjaxResult.success(actTaskService.formDataShow(taskID));
    }

    //保存表单
    @PostMapping(value = "/formDataSave/{taskID}")
    public AjaxResult formDataSave(@PathVariable("taskID") String taskID,
                                   @RequestBody   List<ActWorkflowFormDataDTO> formData ) throws ParseException {
        return toAjax(actTaskService.formDataSave(taskID, formData));

    }

}
